﻿using Company.Config;
using Company.Share.Prism;
using Company.UI.Language;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Company.Main.ViewModels
{
    public class LanguageComponentViewModel : BindableBase
    {
        public IRegionManager RegionManager { get; }
        public LanguageLocalizationManager LanguageManager { get; private set; }
        public ConfigProvider ConfigProvider { get; private set; }
        private int _LanguageSelectedIndex;
        public DelegateCommand<object> LanguageSelectionChangedCommand { get; }
        public DelegateCommand BackToPreviousViewCommand { get; }
        public int LanguageSelectedIndex
        {
            get { return _LanguageSelectedIndex; }
            set { _LanguageSelectedIndex = value; RaisePropertyChanged(); }
        }
        public LanguageComponentViewModel(LanguageLocalizationManager languageManager,
                                          ConfigProvider configProvider,
                                          IRegionManager regionManager)
        {
            RegionManager = regionManager;
            LanguageManager = languageManager;
            ConfigProvider = configProvider;
            LanguageSelectionChangedCommand = new DelegateCommand<object>(LanguageSelectionChanged);
            BackToPreviousViewCommand = new DelegateCommand(BackToPreviousView);
            // 初始化
            Initialize();
            
        }
        /// <summary>
        /// 返回上一个视图
        /// </summary>
        private void BackToPreviousView()
        {
            RegionManager.RequestNavigate(RegionNames.NavigationRegion, ViewNames.SystemSettingView);
        }

        private void Initialize()
        {
            var languageType = ConfigProvider.SystemConfig.LanguageType;
            LanguageManager.SetLanguage(languageType);
            LanguageSelectedIndex = (int)languageType;
        }

        private void LanguageSelectionChanged(object obj)
        {
            LanguageType languageType = (LanguageType)obj;
            LanguageManager.SetLanguage(languageType);
            ConfigProvider.SystemConfig.LanguageType = (LanguageType)obj;
            ConfigProvider.SystemConfigService.SaveToXmlFile(ProfilePaths.SystemXmlConfigFilePath, ConfigProvider.SystemConfig);
        }
    }
}
